package com.atguigu.tingshu.user.service.strategy.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.service.strategy.PaidRecordStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component("1003")  //指定Bean对象ID，跟发货时货物类型一致的
public class VIPPaidRecordStrategy implements PaidRecordStrategy {

    @Autowired
    private UserVipServiceMapper userVipServiceMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;

    /**
     * 处理会员发货逻辑
     * @param userPaidRecordVo
     */
    @Override
    public void handlerPaidRecord(UserPaidRecordVo userPaidRecordVo) {
        log.info("[用户服务]处理会员发货逻辑");
        //3.TODO 处理虚拟物品发货-VIP会员
        //3.0 根据订单编号查询已购声音表判断是否处理过该笔订单
        LambdaQueryWrapper<UserVipService> trackLambdaQueryWrapper = new LambdaQueryWrapper<>();
        trackLambdaQueryWrapper.eq(UserVipService::getOrderNo, userPaidRecordVo.getOrderNo());
        Long count = userVipServiceMapper.selectCount(trackLambdaQueryWrapper);
        if (count > 0) {
            return;
        }
        //3.1 新增会员购买记录
        //3.1.1 初始化已购会员对象
        UserVipService userVipService = new UserVipService();
        //3.1.2 订单编号、用户ID赋值
        userVipService.setUserId(userPaidRecordVo.getUserId());
        userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
        //3.1.3 设置本次会员生效时间以及过期时间
        UserInfo userInfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());
        Boolean isVIP = false;
        Date now = new Date();
        if (userInfo.getIsVip().intValue() == 1 && userInfo.getVipExpireTime().after(now)) {
            isVIP = true;
        }
        //3.1.4 获取购买套餐信息
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(userPaidRecordVo.getItemIdList().get(0));
        Integer serviceMonth = vipServiceConfig.getServiceMonth();
        if (isVIP) {
            //说明当前用户是VIP，获取当前用户会员过期时间+1作为本次会员生效时间+会员时效作为本次会员过期时间
            DateTime startTime = DateUtil.offsetDay(userInfo.getVipExpireTime(), 1);
            userVipService.setStartTime(startTime);
            userVipService.setExpireTime(DateUtil.offsetMonth(startTime, serviceMonth));
        } else {
            //新开会员，获取当前时间作为本次会员生效时间，会员时效作为本次会员过期时间
            userVipService.setStartTime(now);
            userVipService.setExpireTime(DateUtil.offsetMonth(now, serviceMonth));
        }
        //3.1.4 保存会员购买记录
        userVipServiceMapper.insert(userVipService);

        //3.2 更新用户会员标识及会员过期时间
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(userVipService.getExpireTime());
        userInfoMapper.updateById(userInfo);
    }
}
